//
// Created by xuyang on 2018/6/10.
//


#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>

int main(){
    char buffer[2];
    fd_set inputs,testfds;
    struct timeval timeout;
    FD_ZERO(&inputs);
    FD_SET(0,&inputs);
//    int f=open()
    int result,nread;

    int fd=open("./a.txt",O_RDWR,O_NONBLOCK);

//    FD_SET(fd,&inputs);
    while (1){
        testfds=inputs;
        timeout.tv_sec=0;
        timeout.tv_usec=0;
        printf(" before select\n");
        result=select(FD_SETSIZE,&testfds,(fd_set*)0,(fd_set*)0,NULL);
        printf(" after select\n");
        printf("%d\n",result);

        if(result>0){

            if(FD_ISSET(0,&testfds)){
                printf("o读写\n");
                ioctl(0,FIONREAD,&nread);
                if(nread==0){
                    printf("无任何数据输入\n");
                    break;
                };

                while(1){
                    printf("while\n");
                    nread=read(0,buffer,2);
                    if(nread<0){
                        break;
                    }
                    printf("nread %d\n", nread);
                    buffer[nread]=0;
                    printf("read %d from keyboard: %s\n", nread, buffer);
                    nread=-1;
                }
            }
//            if(FD_ISSET(fd,&testfds)){
//                printf("文件读写\n");
//                while(1){
//                    nread=read(fd,buffer,2);
//                    printf("%d\n",nread);
//                    if(nread<0)
//                        break;
//                }
//                printf("read %d from file: %s\n", nread, buffer);
//                break;
//            }
        }else{

        }
    }

    return 0;
}